using System;
using System.Xml;

namespace JamesKovacs.ReSharper.UnitTestSupport.VstsUnit {
    [Serializable]
    public class VstsUnitTestFixtureTask : VstsUnitTestTaskBase, IEquatable<VstsUnitTestFixtureTask> {
        [NonSerialized] private object m_Instance;
        private readonly string m_TypeName;

        public VstsUnitTestFixtureTask(XmlElement element) : base(element) {
            m_TypeName = GetXmlAttribute(element, "TypeName");
            m_AssemblyLocation = GetXmlAttribute(element, "AssemblyLocation");
            m_Explicitly = GetXmlAttribute(element, "Explicitly") == "true";
        }

        public VstsUnitTestFixtureTask(string assemblyLocation, string typeName, bool explicitly) : base("VstsUnit") {
            if(typeName == null) {
                throw new ArgumentNullException("typeName");
            }
            m_AssemblyLocation = assemblyLocation;
            m_TypeName = typeName;
            m_Explicitly = explicitly;
        }

        public bool Equals(VstsUnitTestFixtureTask vstsUnitTestFixtureTask) {
            if(vstsUnitTestFixtureTask != null) {
                if(!base.Equals(vstsUnitTestFixtureTask)) {
                    return false;
                }
                if(Equals(m_AssemblyLocation, vstsUnitTestFixtureTask.m_AssemblyLocation) && Equals(m_TypeName, vstsUnitTestFixtureTask.m_TypeName)) {
                    return (m_Explicitly == vstsUnitTestFixtureTask.m_Explicitly);
                }
            }
            return false;
        }

        public override bool Equals(object obj) {
            return ((this == obj) || Equals(obj as VstsUnitTestFixtureTask));
        }

        public override int GetHashCode() {
            int result = base.GetHashCode();
            result = (0x1d*result) + m_AssemblyLocation.GetHashCode();
            result = (0x1d*result) + m_TypeName.GetHashCode();
            return ((0x1d*result) + m_Explicitly.GetHashCode());
        }

        public override void SaveXml(XmlElement element) {
            base.SaveXml(element);
            SetXmlAttribute(element, "TypeName", TypeName);
            SetXmlAttribute(element, "AssemblyLocation", AssemblyLocation);
            SetXmlAttribute(element, "Explicitly", Explicitly ? "true" : "false");
        }

        public object Instance {
            get { return m_Instance; }
            set { m_Instance = value; }
        }

        public string TypeName {
            get { return m_TypeName; }
        }

        public override string[] MethodNames {
            get { throw new NotImplementedException(); }
        }
    }
}